package app.fugema.shop.main.home.adapter;

import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;

import com.base.common.utils.DensityUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import app.fugema.shop.R;
import app.fugema.shop.databinding.HomeGoodsSeckillItemBinding;
import app.fugema.shop.databinding.HomeGoodsSeckillItemItemBinding;
import app.fugema.shop.main.goods.view.GoodsDetailActivity;
import app.fugema.shop.main.goods.view.GroupBookingActivity;
import app.fugema.shop.main.goods.view.SeckillPrecinctActivity;
import app.fugema.shop.main.home.model.KillMergeGroupOnBean;
import app.fugema.shop.main.order.model.GroupBookingBean;

import java.util.ArrayList;
import java.util.List;

public class HomeGoodsSeckillBaseItemMultiType extends BaseItemMultiType<KillMergeGroupOnBean.DataBean, HomeGoodsSeckillItemBinding> {

    @Override
    public int getLayoutId() {
        return R.layout.home_goods_seckill_item;
    }

    @Override
    public int getItemViewType() {
        return BaseItemTypeInterface.TYPE_HEAD_TWO;
    }

    @Override
    public boolean isFullSpanType() {
        return true;
    }

    @Override
    public void onCreateViewHolder(HomeGoodsSeckillItemBinding binding, BaseViewHolder viewHolder) {
        super.onCreateViewHolder(binding, viewHolder);
        binding.rvRecyclerView.setLayoutManager(new MyLinearLayoutManager(viewHolder.itemView.getContext(), 0, false));
        binding.rvRecyclerView.setAdapter(new SingleRecyclerAdapter<GroupBookingBean.RecordsBean, HomeGoodsSeckillItemItemBinding>() {
            @Override
            public int getLayoutId() {
                return R.layout.home_goods_seckill_item_item;
            }

            @Override
            public void onBindViewHolder(HomeGoodsSeckillItemItemBinding binding, int position, GroupBookingBean.RecordsBean bean) {
                super.onBindViewHolder(binding, position, bean);
                //添加删除线
                binding.rvOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


                if (binding.rclItemView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) binding.rclItemView.getLayoutParams();
                    if (position == getLastPosition()) {
                        if (getItemCount() == 1) return;
                        if (getItemCount() == 3) {
                            marginLayoutParams.rightMargin = 0;
                            marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_5);;
                            binding.rclItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_16));
                            binding.rclItemView.getDelegate().setCornerRadius_BL(DensityUtil.getDimens(R.dimen.dp_16));

                            binding.rclItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_16));
                            binding.rclItemView.getDelegate().setCornerRadius_BR(DensityUtil.getDimens(R.dimen.dp_16));

                            binding.rclItemView.setLayoutParams(marginLayoutParams);
                            return;
                        }
                    }
                    if (position % 2 == 1) {
                        marginLayoutParams.rightMargin = DensityUtil.getDimens(R.dimen.dp_5);
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_f7);
                        binding.rclItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_0));
                        binding.rclItemView.getDelegate().setCornerRadius_BL(DensityUtil.getDimens(R.dimen.dp_0));

                        binding.rclItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_16));
                        binding.rclItemView.getDelegate().setCornerRadius_BR(DensityUtil.getDimens(R.dimen.dp_16));

                    } else {
                        marginLayoutParams.rightMargin = 0;
                        marginLayoutParams.leftMargin = 0;
                        binding.rclItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_16));
                        binding.rclItemView.getDelegate().setCornerRadius_BL(DensityUtil.getDimens(R.dimen.dp_16));

                        binding.rclItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_0));
                        binding.rclItemView.getDelegate().setCornerRadius_BR(DensityUtil.getDimens(R.dimen.dp_0));
                    }

                    binding.rclItemView.setLayoutParams(marginLayoutParams);
                }

            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, GroupBookingBean.RecordsBean bean) {
                super.onItemClick(view, adapter, onclickType, position, bean);
                if (bean.getSkId() == -1) return;
                GoodsDetailActivity.start(view.getContext(), bean);
            }

        });


    }

    @Override
    public void onBindViewHolder(HomeGoodsSeckillItemBinding binding, int position, KillMergeGroupOnBean.DataBean bean) {
        super.onBindViewHolder(binding, position, bean);
        //数据源已做了判断，只会有一个为空
        boolean b1 = UIUtils.isEmpty(bean.getSecKill());
        boolean b2 = UIUtils.isEmpty(bean.getGroupOn());
        if (b1) {
            binding.ivSeckillText.setVisibility(View.GONE);
            binding.llSecKillTime.setVisibility(View.GONE);

            binding.ivGroupToday.setVisibility(View.VISIBLE);
        } else if (b2) {
            binding.ivSeckillText.setVisibility(View.VISIBLE);
            binding.llSecKillTime.setVisibility(View.INVISIBLE);

            binding.ivGroupToday.setVisibility(View.GONE);
        } else {
            binding.ivSeckillText.setVisibility(View.VISIBLE);
            binding.llSecKillTime.setVisibility(View.INVISIBLE);

            binding.ivGroupToday.setVisibility(View.VISIBLE);
        }


        if (binding.rvRecyclerView.getAdapter() instanceof BaseRVAdapter) {
            BaseRVAdapter adapter = (BaseRVAdapter) binding.rvRecyclerView.getAdapter();

            List<GroupBookingBean.RecordsBean> list = new ArrayList<>();
            if (b1) {
                list.addAll(bean.getGroupOn());
            } else if (b2) {
                list.addAll(bean.getSecKill());
            } else {
                if (bean.getSecKill().size() == 1) {
                    GroupBookingBean.RecordsBean recordsBean = new GroupBookingBean.RecordsBean();
                    recordsBean.setSkId(-1);
                    bean.getSecKill().add(recordsBean);
                }
                list.addAll(bean.getSecKill());
                list.addAll(bean.getGroupOn());
            }
            adapter.setDataList(list);
        }
    }


    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, KillMergeGroupOnBean.DataBean bean) {
        super.onItemClick(view, adapter, onclickType, position, bean);
        if (view.getId() == R.id.ivGroupToday) {
            GroupBookingActivity.start(view.getContext());
        } else if (view.getId() == R.id.ivSeckillText) {
            SeckillPrecinctActivity.start(view.getContext());
        }
    }


}
